package com.wqh.zeromq.inform;

import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;

public class Publisher {
    public static void main(String[] args) {
        startPublisher();
    }

    public static void startPublisher() {

        try(ZContext context = new ZContext()){
            //创建“发布/订阅”模式中的“发布”的socket
            ZMQ.Socket publisher = context.createSocket(SocketType.PUB);
            publisher.bind("tcp://*:5555");
            int count =1;

            while(!Thread.currentThread().isInterrupted()){

                Weather weather = new Weather();
                weather.temperature = 16.7f;
                weather.tower = "中央天气局";
                weather.windDirection = 25;
                weather.windSpeed = 10.65f;
                //发送天气信息
                String topicName="Weather";
                //publisher.send(ObjectAndBytes.objectToBytes(weather),0);
                String message=weather.toString()+count;
                publisher.send(topicName+":"+message,0);
                System.out.println(">>"+message);
                count++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
